/**
 *Provider.java
 * 
 * This is the Provider object. Its main purpose is to proved a service i.e.
 * tranportation, food, lodging, etc. The provider is reponsible for checking
 * any conflicts with previous bookings of its service, or for bookings over the
 * maximum capacity of the provider or outside of operating hours.
 *
 * Greg Warner
 * Derek Dupuis
 * Michael Dixey
 * Alex Schwartz
 */ 

import java.util.*;
import java.text.*;

public class Provider
{
   
   String name;
   int capacity;
   GregorianCalendar openTime, closeTime;
   Service service;
   EventColl eventColl;
   ArrayList <Booking> bookingList;
   
   // initializing constructor
   public Provider( String nm, int cap, GregorianCalendar open, 
                   GregorianCalendar close, Service serv )
   {
      name = nm;
      capacity = cap;
      openTime = open;
      closeTime = close;
      service = serv;
      
      eventColl = new EventColl();
      bookingList = new ArrayList <Booking>();
   }
   
   // accessors
   //**************************************************************************    
   public String name() { return name; }
   
   public int capacity() { return capacity; }
   
   public GregorianCalendar openTime() { return openTime; }
   
   public GregorianCalendar closeTime() { return closeTime; }
   
   public Service service() { return service; }
   
   //**************************************************************************
   
   boolean addEvent( Event e )
   {
      return eventColl.add( e );
   }
   
   void addBooking( Booking b )
   {
      bookingList.add( b );
   }
   
   boolean conflict( int cap, GregorianCalendar start, GregorianCalendar end )
   {
      // determine any conflicts with previous bookings, or for bookings over
      // the maximum capacity or outside of operating hours.
      
      int capRemaining;
      GregorianCalendar currTime = (GregorianCalendar)start.clone();
      
      while( currTime.compareTo( end ) < 0 )
      {
         capRemaining = capacity - cap;
         for( int i = 0; i < bookingList.size(); i++ )
         {
            GregorianCalendar bookingStart, bookingEnd;
            bookingStart = bookingList.get( i ).startTime();
            bookingEnd = bookingList.get( i ).endTime();
            
            if( currTime.after( bookingStart ) && currTime.before( bookingEnd ) )
            {
               capRemaining -= bookingList.get( i ).capacity();
            }
         }
         if( capRemaining < 0 )
            return true;
         currTime.add( Calendar.MINUTE, 5 );
      }
      return false;
   }
   
   public String toString()
   {
      // create a string representation of this provider
      String s = "Name: " + name + " Capacity: " + capacity;
      return s;
   }
   
   public String printBookings()
   {
      // create a string representation of this providers bookings
      String s ="";
      
      for( int i = 0; i < bookingList.size(); i++ )
      {
         s = s + bookingList.get( i ).toString();
      }
      
      return s;
   }
}